<?php
namespace app\configs\admin;

use app\admin\controller\Admin;
use think\Db;

use app\common\builder\ZBuilder; // 引入ZBuilder

class Index extends Admin
{
	
    private $type=array(
        '单选',
        '多选',
        '判断'
    );
    private $version=array(
        0=>'通用题库',
        1=>'公共题库',
        2=>'本地题库',
        4=>'新培',
        8=>'复审',
        16=>'旧版',
        32=>'新版',
    );
	//试题分类
    public function index(){
		
        $map=$this->getMap();
        $pguid=$this->request->param("pguid",'1');
        $map[]=["pguid",'eq', $pguid];
        
		$data_list = Db::name('tb_question_type')
		->where($map)
		->select();
		$array = array();//定义新数组

		$btn_child = [
		    'title' => '添加子分类',
		    'icon'  => 'fa fa-fw fa-plus',
		    'href'  => url('add', ['pguid' => '__id__'])
		];
		
		$btn_child_view = [
		    'title' => '查看子分类',
		    'icon'  => 'fa fa-fw fa-ellipsis-v',
		    'href'  => url('index', ['pguid' => '__id__'])
		];

		$btn_question_list = [
		    'title' => '查看试题',
		    'icon'  => 'fa fa-fw fa-th',
		    'href'  => url('question_list', ['id' => '__id__'])
		];
		$adddel=$pguid==1?"add,delete":"delete";
		return ZBuilder::make('table')
			->setPageTitle('试题分类列表')
			->setTableName('tb_question_type')
			->setPrimaryKey('id')	//设置主键
			->addColumns([ // 批量添加列
			        ['id', 'ID'],
			        ['pguid', '上级id'],
			        ['name', '种类'],
			        ['title', '名称'],
			        ['status', '状态', 'switch']
			    ])
			->addColumn('right_button', '操作', 'btn')

			->addRightButtons('edit') // 批量添加右侧按钮1
			->addRightButton('child',$btn_child,1) // 批量添加右侧按钮1
			->addRightButton('child_view',$btn_child_view) // 批量添加右侧按钮1
			->addRightButton('btn_question_list',$btn_question_list) // 批量添加右侧按钮1
			->setRowList($data_list) // 设置表格数据
    		//->setPages($page) // 设置分页数据
			->addTopButtons($adddel) // 添加顶部按钮
			->fetch();
    }
    public function edit_question($id=""){
        if(empty($id)){
            return $this->error("id不能为空");
        }
        $data = Db::name('tb_question')
        ->where("id",$id)
        ->find();
        
        if($this->request->isPost()){
            $data = $this->request->post();
            $data["status"] = isset($data["status"])?"1":"0";
            
            $r = db('tb_question')
            ->where("id",$data["id"])
            ->update($data);
            
            if($r){
                return $this->success("编辑成功",url('question_list',['id'=>$data['question_type']]));
            }else{
                return $this->error("编辑失败",url('question_list',['id'=>$data['question_type']]));
            }
        }
        
        return ZBuilder::make('form')
        ->setPageTitle('修改试题')
        ->addText('title', '标题')
        ->addImages("title_img",'标题--图片')
        ->addRadio('type', '类型','',$this->type)
        ->addLinkages('question_type', '选择类型', '', 'tb_question_type', 4, '','id,title,pguid')
        ->addText('area', '区域')
        ->addText('choice', '选项')
        ->addImages("choice_img",'选项--图片')
        ->addText('answer', '答案')
        ->addText('analysis', '解析')
        ->addImages("analysis_img",'解析--图片')
        
        ->addHidden('id')
        ->setFormData($data)
        //->isAjax(false)
        ->fetch();
    }
    
    public function  question_list($id=null){
        $map=$this->getMap();
        if(! $id){
            return  $this->error("请选择试卷");
        }
        
        $map[]=["a.question_type",'=', $id];
        $data_list = Db::name('tb_question')
        ->alias('a')
        ->join("tb_question_type b",'a.question_type=b.id','left')
        ->field("a.*,b.title titles")
        ->order(['a.id'=>'desc'])
        ->where($map)
        ->paginate();
        // halt(Db::name('tb_question')->getLastSql());
        $edit_question = [
            'title' => '编辑试题',
            'icon'  => 'fa fa-pencil',
            'href'  => url('edit_question', ['id' => '__id__'])
        ];    

        
        return ZBuilder::make('table')
        ->setPageTitle('试题列表')
        ->setTableName('tb_question')
        ->hideCheckbox()
        ->setPrimaryKey('id')	//设置主键
        ->addFilter("type",$this->type)
        ->setSearch(['a.title' => '标题','a.choice' => '选项', 'a.area' => '区域', 'b.title' => '所属类型']) // 设置搜索参数
        ->addColumns([ // 批量添加列
            // ['id', 'ID'],
            ['title', '标题'],
            ['type', '类型',$this->type],
            ['version', '题库',$this->version],
            ['analysis', '解析'],
            ['area', '区域'],
            ['titles', '所属类型'],
            ['answer', '答案']
        ])
        ->addColumn('choice', '选项', 'callback',function($value){
            $str=str_replace('","', "<br>", substr($value,2,-2));
            
            return $str;
            
        })
        ->addColumn('right_button', '操作', 'btn')
        ->addRightButton('edit_question',$edit_question) // 批量添加右侧按钮1
        ->setRowList($data_list) // 设置表格数据
        ->setColumnWidth([
            "title"=>500,
            "type"=>50,
            "analysis"=>100,
            "titles"=>80,
            "answer"=>50,
            "choice"=>200,
            
        ])
        ->fetch();
        
    }
    
    public function add($pguid=1){
	    if($this->request->isPost()){
	        
	        $data = $this->request->post();
	        $data["status"] = isset($data["status"])?"1":"0";
	        $data["guid"]=md5(time()); 
	        $data["pguid"]=$pguid; 
	        $r = db('tb_question_type')
	        ->insert($data);
	        
	        if($r){
	            return $this->success("新增成功",url('index'));
	        }else{
	            return $this->error("新增失败",url('index'));
	        }
	    }
	    
		return ZBuilder::make('form')
			->setPageTitle('添加分类')
			->addText('name', '类型标题')
			->addText('title', '类型名称')
			->addSwitch('status', '是否有效','','1')
			//->isAjax(false)
			->fetch();
	}
	
	
	public function edit($id=""){
		if(empty($id)){
			return $this->error("id不能为空");
		}
		$data = Db::name('tb_question_type')
			->where("id",$id)
			->find();

		if($this->request->isPost()){
		    $data = $this->request->post();
		    $data["status"] = isset($data["status"])?"1":"0";
		    
		    $r = db('tb_question_type')
		    ->where("id",$data["id"])
		    ->update($data);
		    
		    if($r){
		        return $this->success("编辑成功",url('index'));
		    }else{
		        return $this->error("编辑失败",url('index'));
		    }
		}
		
		return ZBuilder::make('form')
			->setPageTitle('修改分类')
			->addText('name', '类型标题')
			->addText('title', '类型名称')
			->addSwitch('status', '是否有效','',1)
			->addHidden('id')
			->setFormData($data)
			//->isAjax(false)
			->fetch();
	}
	
	
	/*public function delete($ids=""){
		$r = db('tb_question_type')
			->where("id",$ids)
			->delete();
			
		if($r){
			return $this->success("删除成功");
		}else{
			return $this->error("删除失败");
		}
	}*/
}
?>